package com.woniuxy.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.woniuxy.entity.Client;
import com.woniuxy.mapper.ClientMapper;
import com.woniuxy.service.ClientService;
import com.woniuxy.utils.KeyUtil;


@Service
public class ClientServiceImpl implements ClientService{
	@Autowired
	private ClientMapper clientMapper;
	
	
	@Autowired
	private RedisTemplate<String, Object> redisTemplate;

	@Override
	public List<Client> findClientOfCname() {
		String key=KeyUtil.generate(this, "findClientOfCname");
		if (redisTemplate.hasKey(key)) {
			List<Object> objects=redisTemplate.opsForList().range(key, 0, redisTemplate.opsForList().size(key));
			List<Client> clients=new ArrayList<Client>();
			ArrayList<Object> data=(ArrayList<Object>) objects.get(0);
			for (Object object : data) {
				clients.add((Client)object);
			}
			return clients;
		}else{
			List<Client> clients=clientMapper.findClientOfCname();
			redisTemplate.opsForList().leftPushAll(key, clients);
			redisTemplate.expire(key, 1, TimeUnit.DAYS);
			return clients;
		}
	}
	

	@Override
	public List<Client> findAllClients() {
		return clientMapper.findAllClients();
	}

	@Override
	public int addClient(Client client) {
		return clientMapper.addClient(client);
	}

	@Override
	public Client findClientByCid(int cid) {
		return clientMapper.findClientByCid(cid);
	}

	@Override
	public int modifyClient(Client client) {
		return clientMapper.modifyClient(client);
	}

	@Override
	public int deleteDataByCid(int cid) {
		return clientMapper.deleteDataByCid(cid);
	}
	
}
